package com.jiuli.liping.service.impl;

import com.jiuli.liping.dao.CommodityDao;
import com.jiuli.liping.dao.ProductDao;
import com.jiuli.liping.dao.ProductOrderDao;
import com.jiuli.liping.dao.ShoppingCartDao;
import com.jiuli.liping.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiuli.liping.service.CommodityService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Author zyl
 * Date  2020-05-18
 */
@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    private CommodityDao commodityDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductOrderDao productOrderDao;

    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Override
    public Commodity get(String id){
        return commodityDao.get(id);
    }
    
    @Override
    public List<Commodity> findList(Commodity commodity) {
        return commodityDao.findList(commodity);
    }
    
    @Override
    public List<Commodity> findAllList() {
        return commodityDao.findAllList();
    }
    
    @Override
    public int insert(Commodity commodity) {
        return commodityDao.insert(commodity);
    }
    
    @Override
    public int insertBatch(List<Commodity> commoditys){
        return commodityDao.insertBatch(commoditys);
    }
    
    @Override
    public int update(Commodity commodity) {
        return commodityDao.update(commodity);
    }
    
    @Override
    public int delete(Commodity commodity) {
        return commodityDao.delete(commodity);
    }

    @Override
    public List<Classification> selectAllCommodity() {
        List<Classification> list = commodityDao.selectAllList();
        for(Classification classification2: list) {
            for (Commodity commodity: classification2.getCommodityList()) {
                Commodity commodity2 = commodityDao.selectOneCommodity(commodity);
                //属性拼接
                //System.out.println(commodity.getCommodityNumber());
                if (commodity2!=null){
                    String spsx = getPropertyNameTo(commodity2);
                    commodity.setSpsx(spsx);
                }

                //当前库存数量,该商品上架总量-冻结-实际核销数量
                String amount = productDao.findSumAmount(commodity.getCommodityNumber());
                String realAmount = productOrderDao.findSumRealAmount(commodity.getCommodityNumber());
                if(amount == null){ amount="0";}if(realAmount == null){ realAmount="0";}
                int fbk2 = Integer.parseInt(amount)-Integer.parseInt(commodity.getFbk3())-Integer.parseInt(realAmount);
                commodity.setFbk2(fbk2+"");
                List<Photograph> list1 = commodityDao.findAllListByPhotograph(commodity.getCommodityNumber());
                commodity.setPhotographList(list1);
                //供应商先不绑定到商品
                //List<Provider> list3 = commodityDao.findAllListByProvider(commodity.getCommodityNumber());
                //commodity.setProviderList(list3);
            }
        }
        return list;
    }

    @Override
    @Transactional
    public int insertCommodity(Commodity commodity) {
        //校验数据库是否存在该商品
        int flag = commodityDao.selectCommodity(commodity);
        if(flag >0){
            return 2;
        }

        String sp = "SP"+System.currentTimeMillis();
        commodity.setCommodityNumber(sp);
        Timestamp curDate = new Timestamp(System.currentTimeMillis());
        commodity.setCreateTime(curDate);

        if(commodity.getPropertyList() != null) {
            for (Property property : commodity.getPropertyList()) {
                property.setCommodityNumber(sp);
            }
            //商品属性信息
            int b = commodityDao.insertProperty(commodity.getPropertyList());
        }

        if(commodity.getProviderList() != null) {
            for (Provider provider : commodity.getProviderList()) {
                provider.setCommodityNumber(sp);
            }
            //商品关联的供应商信息
            int c = commodityDao.insertProvider(commodity.getProviderList());
        }

        if(commodity.getPhotographList() != null) {
            for (Photograph photograph : commodity.getPhotographList()) {
                photograph.setCommodityNumber(sp);
            }
            //商品关联的供应商信息
            int d = commodityDao.insertPhotograph(commodity.getPhotographList());
        }

        //商品基本信息
        int a = commodityDao.insert(commodity);
        return a;
    }

    @Override
    public List<Classification> selectAllCommodityList() {
        List<Classification> list = commodityDao.selectAllCommodityList();
        for(Classification classification2: list) {
            List<Commodity> list2 = new ArrayList<>();
            for (Commodity commodity: classification2.getCommodityList()) {
                //当前库存数量,该商品上架总量-冻结-实际核销数量
                String amount = productDao.findSumAmount(commodity.getCommodityNumber());
                String realAmount = productOrderDao.findSumRealAmount(commodity.getCommodityNumber());
                if(amount == null){ amount="0";}if(realAmount == null){ realAmount="0";}
                int fbk2 = Integer.parseInt(amount)-Integer.parseInt(commodity.getFbk3())-Integer.parseInt(realAmount);
                commodity.setFbk2(fbk2+"");
                List<Photograph> list1 = commodityDao.findAllListByPhotograph(commodity.getCommodityNumber());
                commodity.setPhotographList(list1);
                //查看该商品是否上架
                int a = productDao.getCountCommodity(commodity.getCommodityNumber());
                if(a>0){
                    list2.add(commodity);
                }
            }
            classification2.setCommodityList(list2);
        }
        return list;
    }

    @Override
    public List<Classification> selectAllCommoditySy() {
        List<Classification> list = commodityDao.selectAllCommoditySy();
        for(Classification classif: list) {
            List<Commodity> list3 = new ArrayList<>();
            for (Commodity classification: classif.getCommodityList()) {
                String min = commodityDao.findMinPrice(classification.getCommodityFatherNumber());
                if (min == null) {
                    classification.setPrice("0.00");
                } else {
                    classification.setPrice(min);
                }

                Commodity coo = new Commodity();
                coo.setSalesPrice(min);
                coo.setCommodityFatherNumber(classification.getCommodityFatherNumber());
                List<Photograph> li = commodityDao.findPictureUrl(coo);
                if (li != null && li.size()!=0) {
                    classification.setPicture1(li.get(0).getPhotographUrl());
                }

                int a = productDao.getCountCommodity(classification.getCommodityFatherNumber());
                if (a > 0) {
                    list3.add(classification);
                }
            }

            classif.setCommodityList(list3);
        }
        return list;
    }

    //属性名字转出集合
    public List<String> getPropertyName(Commodity commodity){
        List<String> propertyName = new ArrayList<>();//属性名字
        if(!(StringUtils.isEmpty(commodity.getProperty1Name()))){propertyName.add(commodity.getProperty1Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty2Name()))){propertyName.add(commodity.getProperty2Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty3Name()))){propertyName.add(commodity.getProperty3Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty4Name()))){propertyName.add(commodity.getProperty4Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty5Name()))){propertyName.add(commodity.getProperty5Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty6Name()))){propertyName.add(commodity.getProperty6Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty7Name()))){propertyName.add(commodity.getProperty7Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty8Name()))){propertyName.add(commodity.getProperty8Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty9Name()))){propertyName.add(commodity.getProperty9Name());}
        if(!(StringUtils.isEmpty(commodity.getProperty10Name()))){propertyName.add(commodity.getProperty10Name());}
        return propertyName;
    }

    //属性转出集合
    public List<String> getProperty(Commodity commodity){
        List<String> property = new ArrayList<>();//属性
        if(!(StringUtils.isEmpty(commodity.getProperty1()))){property.add(commodity.getProperty1());}
        if(!(StringUtils.isEmpty(commodity.getProperty2()))){property.add(commodity.getProperty2());}
        if(!(StringUtils.isEmpty(commodity.getProperty3()))){property.add(commodity.getProperty3());}
        if(!(StringUtils.isEmpty(commodity.getProperty4()))){property.add(commodity.getProperty4());}
        if(!(StringUtils.isEmpty(commodity.getProperty5()))){property.add(commodity.getProperty5());}
        if(!(StringUtils.isEmpty(commodity.getProperty6()))){property.add(commodity.getProperty6());}
        if(!(StringUtils.isEmpty(commodity.getProperty7()))){property.add(commodity.getProperty7());}
        if(!(StringUtils.isEmpty(commodity.getProperty8()))){property.add(commodity.getProperty8());}
        if(!(StringUtils.isEmpty(commodity.getProperty9()))){property.add(commodity.getProperty9());}
        if(!(StringUtils.isEmpty(commodity.getProperty10()))){property.add(commodity.getProperty10());}
        return property;
    }

    //属性拼接
    public String getPropertyNameTo(Commodity commodity){
        String name="";
        if(!(StringUtils.isEmpty(commodity.getProperty1Name()))){name+=commodity.getProperty1Name() +":"+commodity.getProperty1()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty2Name()))){name+=commodity.getProperty2Name() +":"+commodity.getProperty2()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty3Name()))){name+=commodity.getProperty3Name() +":"+commodity.getProperty3()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty4Name()))){name+=commodity.getProperty4Name() +":"+commodity.getProperty4()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty5Name()))){name+=commodity.getProperty5Name() +":"+commodity.getProperty5()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty6Name()))){name+=commodity.getProperty6Name() +":"+commodity.getProperty6()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty7Name()))){name+=commodity.getProperty7Name() +":"+commodity.getProperty7()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty8Name()))){name+=commodity.getProperty8Name() +":"+commodity.getProperty8()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty9Name()))){name+=commodity.getProperty9Name() +":"+commodity.getProperty9()+",";}
        if(!(StringUtils.isEmpty(commodity.getProperty10Name()))){name+=commodity.getProperty10Name() +":"+commodity.getProperty10()+",";}
        name = name.substring(0,name.length()-1);
        if(StringUtils.isEmpty(name)){
            name="无";
        }
        return name;
    }

    @Override
    public Message selectCommodityByFatherNumber(Commodity commodity) {
        List<Commodity> commodityList = commodityDao.selectCommodityByFatherNumber(commodity);
        Message mess = new Message();
        if(commodityList!=null){
            for (Commodity commodity2 : commodityList) {
                //属性拼起来
                String propertyname = getPropertyNameTo(commodity2);
                commodity2.setPropertyAll(propertyname);

                String amount = productDao.findSumAmount(commodity2.getCommodityNumber());
                String realAmount = productOrderDao.findSumRealAmount(commodity2.getCommodityNumber());
                if(amount == null){ amount="0";}if(realAmount == null){ realAmount="0";}
                int fbk2 = Integer.parseInt(amount)-Integer.parseInt(commodity2.getFbk3())-Integer.parseInt(realAmount);
                commodity2.setFbk2(fbk2+"");
                commodity2.setFbk2Int(fbk2);
                List<Photograph> list1 = commodityDao.findAllListByPhotograph(commodity2.getCommodityNumber());
                commodity2.setPhotographList(list1);
            }

            ShoppingCart sh = new ShoppingCart();
            sh.setUserNum(commodity.getUserId());
            List<ShoppingCart> list = shoppingCartDao.findAllCartListByNumber(sh);
            if(list!=null){
                mess.setCartNumber(list.size());
            }
            mess.setFlag("1");
            mess.setMessage("数据获取成功");
            mess.setCommodityList(commodityList);
        }else{
            mess.setFlag("2");
            mess.setMessage("数据获取失败");
        }
        return mess;
    }

    @Override
    public List<Classification> getProductByName(Commodity commodity) {
        /*List<Classification> list = commodityDao.getProductByName(commodity);
        for(Classification classification2: list) {
            List<Commodity> list2 = new ArrayList<>();
            for (Commodity com: classification2.getCommodityList()) {
                //当前库存数量,该商品上架总量-冻结-实际核销数量
                String amount = productDao.findSumAmount(com.getCommodityNumber());
                String realAmount = productOrderDao.findSumRealAmount(com.getCommodityNumber());
                if(amount == null){ amount="0";}if(realAmount == null){ realAmount="0";}
                int fbk2 = Integer.parseInt(amount)-Integer.parseInt(com.getFbk3())-Integer.parseInt(realAmount);
                com.setFbk2(fbk2+"");
                List<Photograph> list1 = commodityDao.findAllListByPhotograph(com.getCommodityNumber());
                com.setPhotographList(list1);

                //查看该商品是否上架
                int a = productDao.getCountCommodity(com.getCommodityNumber());
                if(a>0){
                    list2.add(com);
                }
            }
            classification2.setCommodityList(list2);
        }
        return list;*/

        List<Classification> list = commodityDao.getProductByName(commodity);
        //List<Commodity> list2 = new ArrayList<>();
        for(Classification classification: list) {
            List<Commodity> list2 = new ArrayList<>();
            for (Commodity commodity1: classification.getCommodityList()) {
                String min = commodityDao.findMinPrice(commodity1.getCommodityFatherNumber());
                if (min == null) {
                    commodity1.setPrice("0.00");
                } else {
                    commodity1.setPrice(min);
                }

                Commodity coo = new Commodity();
                coo.setSalesPrice(min);
                coo.setCommodityFatherNumber(commodity1.getCommodityFatherNumber());
                List<Photograph> li = commodityDao.findPictureUrl(coo);
                if (li != null) {
                    commodity1.setPicture1(li.get(0).getPhotographUrl());
                }
                int a = productDao.getCountCommodity(commodity1.getCommodityFatherNumber());
                if(a > 0){
                    list2.add(commodity1);
                }
            }
            classification.setCommodityList(list2);
        }
        return list;
    }

}
